<?php

namespace app\app\controller;

use app\app\model\ProjectCBSJModel;
use app\app\model\ProjectPaperModel;
use app\app\model\SysFileInfoModel;
use app\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;


class ProjectReview extends BaseController
{
    public function detail()
    {
        $project_id = input('project_id');
        $list = ProjectCBSJModel::alias('a')->field('a.*,b.sj_dw')
            ->leftjoin('t_project_gjjd b', 'b.project_id = a.project_id')
            ->where('a.project_id', $project_id)->find();

        $list['design_time'] = "2021-08-07 09:51:39";
        $list['ys_file_name'] = '';
        $list['ys_file_path'] = '';
        $list['ns_file_name'] = '';
        $list['ns_file_path'] = '';
        $list['ws_file_name'] = '';
        $list['ws_file_path'] = '';
        $list['cb_file_name'] = '';
        $list['cb_file_path'] = '';
        $list['xjs_file_name'] = '';
        $list['xjs_file_path'] = '';
        if (isset($list['ys_file_id'])) {
            $ys_info = SysFileInfoModel::field('file_name,file_path')->where('file_id', $list['ys_file_id'])->find();
            if ($ys_info) {
                $list['ys_file_name'] = $ys_info['file_name'];
                $list['ys_file_path'] = $ys_info['file_path'];
            }
        }
        if (isset($list['ns_file_id'])) {
            $ns_info = SysFileInfoModel::field('file_name,file_path')->where('file_id', $list['ns_file_id'])->find();
            if ($ns_info) {
                $list['ns_file_name'] = $ns_info['file_name'];
                $list['ns_file_path'] = $ns_info['file_path'];
            }
        }
        if (isset($list['ws_file_id'])) {
            $info = SysFileInfoModel::field('file_name,file_path')->where('file_id', $list['ws_file_id'])->find();
            if ($info) {
                $list['ws_file_name'] = $info['file_name'];
                $list['ws_file_path'] = $info['file_path'];
            }
        }
        if (isset($list['cb_file_id'])) {
            $info = SysFileInfoModel::field('file_name,file_path')->where('file_id', $list['cb_file_id'])->find();
            if ($info) {
                $list['cb_file_name'] = $info['file_name'];
                $list['cb_file_path'] = $info['file_path'];
            }
        }
        if (isset($list['xjs_file_id'])) {
            $info = SysFileInfoModel::field('file_name,file_path')->where('file_id', $list['xjs_file_id'])->find();
            if ($info) {
                $list['xjs_file_name'] = $info['file_name'];
                $list['xjs_file_path'] = $info['file_path'];
            }
        }


//        $list['file_time'] = !empty($list['file_time']) ? date('Y-m-d', strtotime($list['file_time'])) : null;
//        $list['oper_time'] = !empty($list['oper_time']) ? date('Y-m-d', strtotime($list['oper_time'])) : null;
//        $list['design_time'] = !empty($list['design_time']) ? date('Y-m-d', strtotime($list['design_time'])) : null;
//        $list['ys_time'] = !empty($list['ys_time']) ? date('Y-m-d', strtotime($list['ys_time'])) : null;
//        $list['ns_time'] = !empty($list['ns_time']) ? date('Y-m-d', strtotime($list['ns_time'])) : null;
//        $list['ws_time'] = !empty($list['ws_time']) ? date('Y-m-d', strtotime($list['ws_time'])) : null;

        $this->success('success', $list);
    }

    /**
     * 编辑初步评审
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function edit()
    {
        $id = input('id', '');
        if (empty($id)) $this->error('请传入id');

        $param = $this->request->param();

        $data = ProjectCBSJModel::where('id', $id)->find();
        if (empty($data)) $this->error('该数据不存在');

        $sj_dw = input('sj_dw', '');
        Db::table('t_project_gjjd')->where('project_id', $data->project_id)->update(['sj_dw' => $sj_dw]);

        $data->save($param);

        $this->success('success');
    }
}
